package fun.tan90.device.source.mysql.plugin.model;

import com.github.shyiko.mysql.binlog.event.DeleteRowsEventData;
import com.github.shyiko.mysql.binlog.event.EventData;
import com.github.shyiko.mysql.binlog.event.UpdateRowsEventData;
import com.github.shyiko.mysql.binlog.event.WriteRowsEventData;
import lombok.Data;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

@Data
public class RowMutationEventData {

    private long tableId;

    private List<Serializable[]> insertRows;

    private List<Serializable[]> deleteRows;

    private List<Map.Entry<Serializable[], Serializable[]>> updateRows;

    public RowMutationEventData(EventData eventData) {
        if (eventData instanceof UpdateRowsEventData updateRowsEventData) {
            this.tableId = updateRowsEventData.getTableId();
            this.updateRows = updateRowsEventData.getRows();
            return;
        }
        if (eventData instanceof WriteRowsEventData writeRowsEventData) {
            this.tableId = writeRowsEventData.getTableId();
            this.insertRows = writeRowsEventData.getRows();
            return;
        }
        if (eventData instanceof DeleteRowsEventData deleteRowsEventData) {
            this.tableId = deleteRowsEventData.getTableId();
            this.deleteRows = deleteRowsEventData.getRows();
        }
    }
}